	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.11.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using data files in $dir: 
	****
	****  	pers-use.dta
	**** 	worlddata.dta
	****	worldcoor.dta
	
	*********************************************************************************
		capture log close
		log using Ch2.log, replace

	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"
		
		*****************************
		**** Endogenous populism ****
		*****************************
			* AKP example *
			use erdogan-populism,clear
			gen  pub = substr(publication ,1,10)
			tab pub
			egen yrpopulist= mean(proportion),by(year)
			egen tag = tag(year)
			keep if tag==1 & year<2017
			sort year
			save temp,replace
			use pers-use,clear
			qui sum polar
			gen polar01=(polar+abs(r(min)))/(r(max)+abs(r(min)))
			gen akp_pop = .796 if year==2002 & country=="Turkey"
			replace akp_pop = .796 if year==2007 & country=="Turkey"
			replace akp_pop = .952 if year==2011 & country=="Turkey"
			replace akp_pop = .962 if year==2015 & country=="Turkey"
			sort year 
			merge year using temp
 
			twoway (line v2x_poly year if country=="Turkey",lcol(gs8)lpat(dash)sort ylab(.2(.2)1) ///
				tit(Democratic backsliding in Turkey)xline(2003,lcol(gs1)) xtit(Year) ///
				ytit(Electoral democracy) 	///
				text(.45 2000.75 "Erdogan selected" "Prime Minister {&rarr}",size(vsmall))  ///
				legend(lab(1 "Electoral democracy")lab(2 "Expert-coded AKP populism")lab(3 "AKP populist news stories") ///
				order(1 2 3)size(vsmall)col(1)pos(11)ring(0))) ///
				(scatter akp_pop year if country=="Turkey",col(gs1)) ///
				(line akp_pop year if country=="Turkey",sort lpat(line)lcol(gs1)) ///
				(lpoly yrpop year if country=="Turkey",bw(2) lpat(line)lcol(gs1)yaxis(2) ///
				ytit("Share of AKP news stories" "describing Erdogan as {it:populist}",size(small)axis(2))) 
			gr export "$dir\golden\Ch2-AKP-Populist.pdf",as(pdf)replace 

			xtset cowcode year
			reg yrpop l1v2x_poly if country=="Turkey",
			est store akp1
			abar,lags(5) 
			reg yrpop l1v2x_poly l1.yrpop if country=="Turkey",
			est store akp2
			reg d.yrpop d.l1v2x_poly if country=="Turkey",
			est store akp3
			reg d.yrpop d.l1v2x_poly l1.yrpop if country=="Turkey",
			est store akp4
 			reg d.yrpop d.l1v2x_poly l1.yrpop if country=="Turkey" & year<2016
			est store akp5
			
			local var  = "v2x_polyarchy v2x_libdem v2x_partipdem"
			foreach v of local var {
				qui var yrpop  `v' if country=="Turkey" , lags(1/1) dfk small
				vargranger			
			}
			erase temp.dta

			**************************************************************************************************************************
			**** Change in democracy level year prior to election is correlated with change in populism score for incumbent party ****
			**************************************************************************************************************************
			use pers-use,clear
			tsset cowcode year
			gen devpop = d.populism if v2paid == l.v2paid  /* same ruling party year-to-year */
			gen dev1dem  =d.l1v2x_poly
			krls devpop dev1dem if (v2xel_elecpres ==1 | v2xel_elecparl ==1)
			* Correctly adjust for lagged outcome for ceiling effect *
			krls devpop dev1dem l1pop if (v2xel_elecpres ==1 | v2xel_elecparl ==1)
			krls devpop dev1dem l1pop l2v2x_poly if (v2xel_elecpres ==1 | v2xel_elecparl ==1)
			krls devpop dev1dem l1pop l2v2x_poly pres if (v2xel_elecpres ==1 | v2xel_elecparl ==1)
			krls devpop dev1dem l1pop if (v2xel_elecpres ==1 | v2xel_elecparl ==1) & create==1
			krls devpop dev1dem l1pop if (v2xel_elecpres ==1 | v2xel_elecparl ==1) & create==0

 
	****************************
	*** Descriptive patterns ***
	****************************
		** Distribution **
		use pers-use,clear
		keep if year>=1991
		sfrancia persparty v2x_polyarchy
		hist persparty, bin(25)xtit(Party personalism)ytit(Density)col(gs12) ///
			kden kdenopts(bw(.1)lcol(blue)lpat(solid)) ///
			normal normopts(lcol(red)lpat(solid))  ///
			text(3.5 .7 "Shapiro–Francia test W'=0.981") ///
			text(2.9 .7 "NxT=2,392") tit("Democracies, 1991-2020")
	 	graph export "$dir\golden\T-PersParty-Distribution.pdf", as(pdf)   replace
	
		*************************************************
		*** Party personalism by region and over time ***
		*************************************************
		use pers-use,clear
		gen d1 = gwf_duration
		gen d2 = gwf_duration^2
		gen d3 = gwf_duration^3
		label var persparty "Party personalism"
		label var e_migdppcln "GDP pc"
		label def region 1 "Eastern Europe and Central Asia" 2 "Latin America and the Caribbean"  ///
			3 "Middle East and North Africa" 4 "Sub-Saharan Africa" 5 "Western Europe and North America" ///
			6 "Asia and Pacific" ,replace
		label val pregion region
		reg persparty d1 d2 d3, 
		qui predict r1 if e(sample)==1,res
		qui sum r1
		qui replace r1 = r1+abs(r(min))
		qui sum r1
		qui replace r1 = r1/abs(r(max))
		table pregion if year==min,stat(mean persparty ivdem ld e_migdppcln r1)nformat(%9.2f)
		
		local var = "persparty ivdem gwf_duration e_migdppcln r1"
		foreach v of local var {
			qui sum `v'
			replace `v'  = (`v'-r(mean))/r(sd)
		}
		sum persparty ivdem gwf_duration  e_migdppcln r1
		egen yrcreate = mean(create),by(year)

		twoway (lpoly create year,bw(2)lcol(gs1)xtit(Year)yaxis(2)ylab(0(.1).5,axis(2)) ///
			ytit(Share of leaders who create their own party,axis(2))tit(Global trends,size(large)))   ///
			(lpoly ivdem year,bw(2)lpat(dash)lcol(gs10)) ///
			(lpoly gwf_duration  year,bw(2)lpat(solid)lcol(gs10) ///
			ytit(Scaled indices,height(1))legend(lab(2 "Democracy level") ///
			lab(3 "Democracy age")lab(1 "Leader creates own party") ///
			pos(5)ring(0))saving(h1.gph,replace)ylab(-.2(.1).2)yscale(range(-.15 .23)))
		twoway (lpoly persparty year,bw(2)lpat(dash) lcol(gs1)tit(Party personalism increasing over time,size(large)))   ///
			(lpoly r1 year,bw(2) lpat(solid)lcol(gs1)xtit(Year) ///
			ytit("")legend(lab(1 "Party personalism") ///
			lab(2 "Adjusted party personalism") ///
			pos(5)ring(0))saving(h2.gph,replace)ylab(-.2(.1).2)yscale(range(-.15 .23)))
		gr combine h1.gph h2.gph,col(2)xsize(8)
		erase h1.gph
		erase h2.gph
		gr export "$dir\golden\Ch2-Time-Trends.pdf",as(pdf)replace 
	
		**************
		**** Maps ****
		**************
		use pers-use,clear
		keep if gwf_regime=="democracy" | gwf_regime=="provisional"
		drop tag
		recode persparty (0=.0001) (1=.9999)
		* Adjusted party personalism score *
			qui reg persparty ivdem ld,
			qui predict xb if e(sample)==1,xb
			qui gen r1 = persparty-xb
			qui sum r1
			qui replace r1 = r1+abs(r(min))
			qui sum r1
			qui replace r1 = r1/abs(r(max))
			qui reg persparty ivdem ld,
			qui predict r2 if e(sample)==1,res
			qui sum r2
			qui replace r2 = r2+abs(r(min))
			qui sum r2
			qui replace r2 = r2/abs(r(max))
		egen demcount=count(year) if year>=1991 & year<=2020,by(cowcode)
		gen demint=demcount/30
		keep if country~="" & persparty~=.
		egen avepparty =mean(persparty),by(cowcode)
		egen aver1=mean(r1),by(cowcode)
		egen tag=tag(cowcode) if avepparty~=. & demint~=.
		gen NAME = country
		replace NAME  = "Central African Rep." if country=="Central African Republic"
		replace NAME  = "Czechia" if country=="Czech Republic"
		replace NAME  = "Dominican Rep." if country=="Dominican Republic"
		replace NAME  = "Côte d'Ivoire" if country=="Ivory Coast"
		replace NAME  = "Serbia" if country=="Federal Republic of Yugoslavia"
		replace NAME  = "Guinea-Bissau" if country=="Guinea Bissau"
		replace NAME  = "Congo" if country=="Republic of Congo"
		replace NAME  = "United States of America" if country=="United States"
		keep if tag==1
		gen rdemint=round(demint,.01)
		replace demint=rdemint
		gen ravepparty = round(avepparty,.01)
		replace avepparty = rave
		gen raver1 = round(r1,.01)
		replace aver1 = raver1
		keep country NAME avepparty demint aver1
		sort NAME
		merge NAME using "worlddata.dta"
		tab _merge
		list country if _merge==1
		drop remitgdp _merge
		format demint avepparty %5.2f
		spmap demint using worldcoor.dta if SOVEREIGNT!="Antarctica", ///
			id(id)ndf(gs12) ndocolor(gs1)clmethod(kmeans)clnum(5)fcolor(Greys)  ///
			title("Share of years coded as democracy",size(*0.8)) ///
			subtitle("1991-2020", size(*0.8))  ///
			legstyle(3) legend(ring(0) position(8)) ///
			graphregion(icolor(white)margin(tiny)) ///
			plotregion(icolor(white)margin(tiny))  
		gr export "$dir\golden\Ch1-MapDem.pdf",as(pdf)replace 
		spmap avepparty using worldcoor.dta if SOVEREIGNT!="Antarctica", ///
			id(id)ndf(gs15)clmethod(kmeans)clnum(5)fcolor(Oranges)  ///
			title("Average level of party personalism",size(*0.8)) ///
			legstyle(3) legend(ring(0)position(8))saving(h1.gph,replace) ///
			graphregion(icolor(white)margin(tiny)) ///
			plotregion(icolor(white)margin(tiny))  
		spmap aver1 using worldcoor.dta if SOVEREIGNT!="Antarctica", ///
			id(id)ndf(gs15)clmethod(kmeans)clnum(5)fcolor(Oranges)  ///
			title("Adjusted level of party personalism",size(*0.8)) ///
			legstyle(3) legend(ring(0)position(8))saving(h2.gph,replace) ///
			graphregion(icolor(white)margin(tiny)) ///
			plotregion(icolor(white)margin(tiny)) 
		gr combine h1.gph h2.gph,col(1) xsize(4) ysize(4)
		gr export "$dir\golden\T-MapParties.pdf",as(pdf)replace 
		global d = "persparty"
 
		***************************************************
		**** Personalist parties and economic ideology ****
		***************************************************
				use pers-use,clear
				global x = "ld ivdem pres mixed proportional"
				gen s = year==min | v2xel_elecpres==1|  v2xel_elecparl==1
				gen proportional= v2elparlel==1 | v2elparlel==3
				gen mixed =  v2elparlel==2
			*** Personalist parties and left-right ideology ***
				desc   v2pawelf v2pariglef
				qui sum v2pariglef
				replace v2pariglef= (v2pariglef+abs(r(min)))
				qui sum v2pariglef
				replace v2pariglef=v2pariglef/r(max)	
				twoway (hist persparty,col(gs12)yscale(range(0 12)axis(2))yaxis(2)bin(50) ///
					ylab(0 "",axis(2)nolabels noticks) ytitle("",axis(2)) yscale(range(0 100)axis(2))) ///
					(lpolyci v2pariglef persparty,bw(.1)ytitle("{&larr} Left                          Right {&rarr}") ///
					yscale(alt)yscale(range(.4 .7))ylab(.4(0.1)0.7, axis(1)) ///
					xtit(Party personalism)legend(off)tit(Left-right economic ideology,size(large)) saving(h3.gph,replace) ///
					yline(.5824661,lcol(blue*.5)) text(.57 .3 "Ideology mean",size(vsmall))  )	
				ttest v2pariglef,by(create)
				
			*** Personalist parties and welfare statism ideology ***
				desc v2pawelf
				qui sum v2pawelf
				replace v2pawelf= (v2pawelf+abs(r(min)))
				qui sum v2pawelf
				replace v2pawelf=v2pawelf/r(max)	
				twoway (hist persparty,col(gs12)yscale(range(0 12)axis(2))yaxis(2)bin(50) ///
					ylab(0 "",axis(2)nolabels noticks) ytitle("",axis(2)) yscale(range(0 100)axis(2))) ///
					(lpolyci v2pawelf persparty,bw(.1)ytitle("{&larr} Less welfare statist          More welfare statist {&rarr}") ///
					yscale(alt)yscale(range(.4 .7))ylab(.4(0.1)0.7, axis(1)) ///
					xtit(Party personalism)legend(off)tit(Welfare state ideology,size(large)) saving(h4.gph,replace) ///
					yline(.578,lcol(blue*.5)) text(.565 .02 "Welfare" "statist" "mean",size(vsmall))  )	
				ttest v2pawelf,by(create)
 
 				global x = "ld ivdem pres mixed proportional"
				reghdfe v2pariglef persparty $x if s==1,a(period pregion)cluster(lid)
				reghdfe v2pawelf   persparty $x if s==1,a(period pregion)cluster(lid)

			gr combine h3.gph h4.gph,col(3) xsize(8) ysize(4) iscale(.8)
			gr export "$dir\golden\Ch2-Persparty-Ideology.pdf",as(pdf)replace 
			
			qui sum v2pariglef if s==1
			qui gen extreme = v2pariglef<r(mean)-r(sd) |  v2pariglef>r(mean)+r(sd) if v2pariglef~=.
			qui sum v2pariglef if s==1
			qui gen extreme2 = v2pariglef<r(mean)-(1.5*r(sd)) |  v2pariglef>r(mean)+(1.5*r(sd)) if v2pariglef~=.
			qui sum v2pariglef if s==1
			qui gen extreme3 = abs((v2pariglef-r(mean))/r(sd)) if v2pariglef~=.
			tab extreme extreme2 if s==1
			reghdfe extreme i.period persparty $x if s==1,a(cowcode)cluster(lid)
			reghdfe extreme2 i.period persparty $x if s==1,a(cowcode)cluster(lid)
			reghdfe extreme3 i.period persparty $x if s==1,a(cowcode)cluster(lid)
			gen lnextreme3 = ln(1+extreme3)
			reghdfe lnextreme3 i.period persparty $x if s==1,a(cowcode)cluster(lid)
			
			* Populism is associated with ideological extremism but not personalist parties *
			probit extreme persparty $x if s==1,cluster(lid)
			probit extreme populism $x if s==1,cluster(lid)
			margins,dydx(populism)
			probit extreme persparty populism $x if s==1,cluster(lid)
			margins,dydx(populism persparty)
		
			*** Personalism less likely to be workingclass ***
			gen workingclass = v2pagroup_8==1 | v2pagroup_10==1 if v2paid~=.
			qui centile v2xpa_popul if s==1,centile(50)
			local c = r(c_1)
			gen hipop = v2xpa_popul>`c' if v2xpa_popul~=.
			qui centile persparty if s==1,centile(50)
			local c = r(c_1)
			gen hipers = persparty>`c' if persparty~=.
			tab hipers hipop 
			gen e = .
			gen hi = . 
			gen lo = .
			gen n = _n
			ttest workingclass,by(hipers)
			local  m1=r(mu_1) 
			local se1 = r(sd_1)/(sqrt(r(N_1)))
			local  m2=r(mu_2)
			local se2 = r(sd_2)/(sqrt(r(N_2)))
			replace e=`m1' if _n==1
			replace hi = `m1' + 1.95* `se1' if _n==1
			replace lo = `m1' - 1.95* `se1'  if _n==1
			replace e=`m2' if _n==2
			replace hi = `m2' + 1.95*`se2' if _n==2
			replace lo = `m2' - 1.95*`se2' if _n==2	
			ttest workingclass,by(hipop)
			local  m1=r(mu_1) 
			local se1 = r(sd_1)/(sqrt(r(N_1)))
			local  m2=r(mu_2)
			local se2 = r(sd_2)/(sqrt(r(N_2)))
			replace e=`m1' if _n==4
			replace hi = `m1' + 1.95* `se1' if _n==4
			replace lo = `m1' - 1.95* `se1'  if _n==4
			replace e=`m2' if _n==5
			replace hi = `m2' + 1.95*`se2' if _n==5
			replace lo = `m2' - 1.95*`se2' if _n==5	
			twoway (bar e n if n<=5,barwidth(.5)bcol(gs13)ytit("Share of parties with {it:working-class} support")) ///
				(rspike hi lo n if n<=5,ylab(0(.05).2)col(gs1)legend(off)xtit("")tit(Working-class party support) ///
				xlab(1 `""{bf:Low} party" "personalism""' 2 `""{bf:High} party" "personalism""' ///
				4 `""{bf:Low} party" "populism""' 5  `""{bf:High} party" "populism""')xscale(range(0.5 5.5)))
			probit workingclass ld persparty if s==1,cluster(lid)
			probit workingclass ld populism if s==1,cluster(lid)
			probit workingclass ld persparty populism if s==1,cluster(lid)
				* pattern remains in FE model *
			reghdfe workingclass ld persparty if s==1,a(cowcode year)cluster(lid)
			reghdfe workingclass ld populism if s==1,a(cowcode year)cluster(lid)
			reghdfe workingclass ld persparty populism if s==1,a(cowcode year)cluster(lid)
			
			*** Personalist parties less likely to mobilize on economic issues ***
			glm v2pasalie_10 ivdem ld create if s==1,fam(bin)link(probit)cluster(lid)
 			reghdfe v2pasalie_10 ivdem ld persparty if s==1,a(cowcode year)cluster(lid)
 			reghdfe v2pasalie_10 ivdem ld populism if s==1,a(cowcode year)cluster(lid)
 			reghdfe v2pasalie_10 ivdem ld persparty populism if s==1,a(cowcode year)cluster(lid)
			
			reg workingclass ld persparty if s==1,cluster(lid)
			est store pp1
			reg workingclass ld populism if s==1,cluster(lid)
			est store pp2
			reg workingclass ld persparty populism if s==1,cluster(lid)
			est store pp3
			reghdfe workingclass ld persparty if s==1,a(cowcode year)cluster(lid)
			est store pp4
			reghdfe workingclass ld populism if s==1,a(cowcode year)cluster(lid)
			est store pp5
			reghdfe workingclass ld persparty populism if s==1,a(cowcode year)cluster(lid)
			est store pp6
			label var ld `""Democracy" "age       ""'
			label var persparty `""Party      " "{bf:personalism}""'
			label var populism `""Party   " "populism""'
		coefplot (pp1, msymbol(d))(pp2, msymbol(T))(pp3, msymbol(P)), order(persparty populism ld)  ///
			drop(_cons) xline(0)  mfcolor(white) grid(glcolor(gs15)) ///
			levels(95 90) legend(off) xsize(2) ysize(2) xlab(-.25(.25).5)  ///
			xtitle("        Coefficient estimate", size(small))saving(h1.gph,replace)  ///
			ciopts(lwidth(thin)) aspectratio(1.1) scale(.75) title("Working class support: No FE", size(medium) height(2))
		coefplot (pp4, msymbol(d))(pp5, msymbol(T))(pp6, msymbol(P)), order(persparty populism ld)  ///
			drop(_cons) xline(0)  mfcolor(white) grid(glcolor(gs15)) ///
			levels(95 90) legend(off) xsize(2) ysize(2) xlab(-.25(.25).5)  ///
			xtitle("        Coefficient estimate", size(small))saving(h2.gph,replace)  ///
			ciopts(lwidth(thin)) aspectratio(1.1) scale(.75) title("Working class support: Two-way FE", size(medium) height(2))
		gr combine h1.gph h2.gph,xsize(8)tit(Populism - but not personalism - associated with working class support)iscale(1.1)
		gr export "$dir\golden\T-Personalism-Populism-Working-Class.pdf",as(pdf)replace 


	*************************************************
	*** Personalist parties and electoral systems ***
	*************************************************
		use pers-use,clear
		gen lowerprop = v2elparlel==1 if v2elparlel~=.
		gen lowermix = v2elparlel==2 if v2elparlel~=.
		gen lowermajor = v2elparlel==0 if v2elparlel~=.
		
		* Only four countries change pres vs. parl *
		egen m_pres = mean(pres) if year==min,by(cowcode)
		tab country_name if m_pres>0 & m_pres<1
		egen ctag =tag(country) if pres~=.
		tab pres if ctag==1 & (m_pres==1 | m_pres==0)
		drop m_pres	ctag
		
		* Presidential systems are not just as likely in majoriatarian systems *
		tab  v2elparlel pres, col
		egen m_v2elparlel=mean(v2elparlel),by(cowcode)
		tab m_v2elparlel
		tab country if m_v2elparlel~=0 & m_v2elparlel~=1 & m_v2elparlel~=2
		
		*** Majoritarian lower house systems becoming less common ***
		gen time = year-1990
		mlogit v2elparlel time ld i.pregion if v2elparlel~=3,cluster(cowcode)base(0)
		
		* Reported models *
		reg persparty pres if  year==min,cluster(cowcode)
		est store sys1
		reg persparty pres ld if  year==min,cluster(cowcode)
		est store sys2
		reg persparty lowerprop lowermix if v2elparlel~=3 & year==min,cluster(cowcode)
		est store sys3
		reg persparty lowerprop lowermix ld if v2elparlel~=3 & year==min,cluster(cowcode)
		est store sys4
		reg persparty lowerprop lowermix ld pres if v2elparlel~=3 & year==min,cluster(cowcode)
		est store sys5
		label var lowermix `""Mixed      " "(Majoritarian)""'
		label var lowerprop `""Proportional  " "(Majoritarian)""'
		label var ld `""Age of    " "democracy""'
		label var pres `""Presidential" "system    ""'
		coefplot(sys1, msymbol(O))(sys2, msymbol(T)) , ///
			drop(_cons) grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.1(.1).2) ///
			xtitle(Coefficient estimates) order(remit)level(95 90) title("Presidents and prime ministers", ///
			size(medium)height(6))xsize(2) ysize(2.5) mlabel format(%9.2g) ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.65) ///
			legend(lab(3 "Bivariate")lab(6 "    Adjust for" "democracy age")  ///
			order(3 6)size(vsmall)pos(5)col(1)ring(0))saving(h1.gph,replace)	
		coefplot(sys3, msymbol(O))(sys4, msymbol(T)) (sys5, msymbol(d))  , ///
			drop(_cons) order(lowerprop lowermix pres ld) ///
			grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.1(.1).2) ///
			xtitle(Coefficient estimates)  level(95 90) title("Electoral systems", ///
			size(medium)height(6)) xsize(2) ysize(2.5) mlabel format(%9.2g) ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.65)legend(lab(3 "Bivariate") ///
			lab(6 "    Adjust for" "democracy age")lab(9 "  Presidential" "+ democracy age") ///
			size(vsmall)order(3 6 9)pos(5)col(1)ring(0)) ///
			saving(h2.gph,replace)
		gr combine h1.gph h2.gph
		gr export "$dir\golden\Ch2-electoral-system-persparty.pdf",as(pdf)replace 

		 *** Incentives to cultivate a personalist vote ***
		 sum pers_rank dom_rank
		 qui replace pers_rank=pers_rank/12
		 qui replace dom_rank=dom_rank/12
		 reg persparty dom_rank,cluster(cowcode)
		 est store cult1a
		 reg persparty dom_rank ld,cluster(cowcode)
		 est store cult2a
		 reg persparty dom_rank ld pres,cluster(cowcode)
		 est store cult3a
		 reghdfe persparty dom_rank ld pres,a(pregion) cluster(cowcode)
		 est store cult4a
		 
		 reg persparty pers_rank,cluster(cowcode)
		 est store cult1b
		 reg persparty pers_rank ld,cluster(cowcode)
		 est store cult2b
		 reg persparty pers_rank ld pres,cluster(cowcode)
		 est store cult3b
		 reghdfe persparty pers_rank ld pres,a(pregion)cluster(cowcode)
		 est store cult4b
		 label var propn "Proportional"
		 label var pers_rank `""Personal" "vote rank""'
		coefplot(cult1b, msymbol(O))(cult2b, msymbol(T))(cult3b, msymbol(S)) (cult4b, msymbol(D)), drop(_cons) ///
			grid(glcolor(gs15))xline(0,lpattern(dash)) ///
			xtitle(Coefficient estimates) order(remit)level(95 90) ///
			title("Incentives to cultivate a personalist vote" "and selection into personalist party rule", ///
			size(medium)height(6)) legend(lab(3 "Bivariate") lab(6 "+ democracy age") lab(9 "+ Presidential system") lab(12 "+ Region FE") ///
			pos(6)ring(1)col(2)) xsize(2) ysize(2.5) mlabel format(%9.2g) ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.65)
		gr export "$dir\golden\T-Cultivate-persparty.pdf",as(pdf)replace 
		
twoway (scatter   persparty pers_rank) (lpolyci   persparty pers_rank,legend(off)xtit(Electoral rules to cultivate a personal vote)ytit("Party personalism ({&theta})"))
twoway (scatter   persparty dom_rank) (lpolyci   persparty dom_rank,legend(off)xtit(Electoral rules to cultivate a personal vote)ytit("Party personalism ({&theta})"))		


		******************************************		
		**** Personalist parties and populism ****
		******************************************
			use pers-use,clear
			egen max=max(year),by(lid)
			gen ddem = v2x_poly-ivdem if year==max
			egen devdem = max(ddem),by(lid)
			gen rho =.
			gen n =_n

			* Party populism *
			krls persparty populism
			mat b = e(b)
			local b = b[1,1]
			replace rho = `b' if n==1
			krls persparty populism if year==min
			mat b = e(b)
			local b = b[1,1]
			replace rho = `b' if n==2
			krls persparty populism ld ivdem if year==min,d(k)
			mat b = e(b)
			local b = b[1,1]
			replace rho = `b' if n==3
		 
			twoway bar rho n if n<4,ytit({&rho})barw(.4) xtit("")ylab(0 (.1) .3) /// 
				xlab(1  `""All leaders" "All years""'  2 `""All leaders" "First year""'    ///
				3 `""All leaders (531)" "First year" "Adjust for" "consolidation" "', ///
				labsize(vsmall))xscale(range(0.5 3.5))ylab(0 (.05) .25)bcol(gs12) ///
				text(0.26 1  "0.28*",size(small)) text(0.17 2  "0.19*",size(small)) ///
				text(0.04 3  "0.05",size(small)) tit(Correlation between party personalism and populism) ///
				note("* {it:p} < 0.05",size(vsmall))
			gr export "$dir\golden\Ch2-Persparty-Populist.pdf",as(pdf)replace 
 
			* Point-wise marginal effect decreases as democracy change increases *
			twoway lpoly k_populism devdem,legend(off)bw(.1)xtit("Change in democracy score during leader's tenure") ///
				ylab(0(.05).25)ytit("{&rho}",size(large))yline(0,lcol(red)) ///
				tit("Correlation ({&rho}) between party {it:personalism{sub:i,t=0}} and {it:populism{sub:i,t=0}}")
			gr export "$dir\golden\T-Endogneous-Populism.pdf",as(pdf)replace 
				
			* Correlation in backsliders but not others *
			qui sum devdem if populism~=. & year==min
			gen bad = devdem<r(mean) -r(sd)  if devdem~=.
			tab bad if year==min
			krls persparty populism ld ivdem if year==min & bad==1
			krls persparty populism ld ivdem if year==min & bad==0
				
			* Populism and party personalism do not correspond in Brazil and Colombia *
			twoway (line populism year if country=="Brazil",sort)   ///
				(line persparty year if country=="Brazil",sort ytitle(Level)xtit(Year)legend(lab(1 "Populism") ///
				lab(2  "Party personalism")pos(6)col(3))tit(Brazil))
			twoway (lpoly populism year if country=="Colombia",sort) ///
				(lpoly persparty year if country=="Colombia",sort ytitle(Level)xtit(Year)legend(lab(1 "Populism") ///
				lab(2  "Party personalism")pos(6)col(3))tit(Colombia))
				
	  	*************************************************		
		**** Selecting into ruling party personalism ****
		*************************************************
		use pers-use,clear
		gen majoritarian=v2elparlel==0 if v2elparlel~=.
		gen pubfin = l1v2elpubfin_ord==3 | l1v2elpubfin_ord==4 if l1v2elpubfin_ord~=. & l1v2elpubfin_ord~=2
		gen proportional= v2elparlel==1 | v2elparlel==3
 		qui gen wealth=e_migdppcln
		qui xi:reg e_migdppcln i.cowcode i.year 
		qui predict hat
		replace wealth=hat if wealth==.
		hist wealth
		sum wealth e_migdppcln
		egen cmin = min(year),by(cowcode)
		gen osupdem=l1supdem if year==min
		egen isupdem = max(osupdem),by(lid)
		gen devsupdem = supdem-l4supdem
		
		local var = "ld l1v2elpubfin l1polar l12gr l1supdem l1v2xps_party wealth"
		foreach v of local var {
			egen z`v' = std(`v')
		}
		keep if year==minyr
		replace zwealth=zwealth/.5
		
		* Dynamic correlates of selection into ruling party personalism *
		reghdfe persparty zld zl1polar zl1v2elpubfin zwealth zl1v2xps_party,a(year)cluster(lid)
		est store s1a
		reghdfe persparty zld zl1polar zl1v2elpubfin zwealth zl1supdem zl1v2xps_party,a(year)cluster(lid)
		est store s1b
		reghdfe persparty zld zl1polar zl1v2elpubfin zwealth zl1v2xps_party,a(cowcode year)cluster(lid)
		est store s2a
		reghdfe persparty zld zl1polar zl1v2elpubfin zwealth zl1supdem zl1v2xps_party,a(cowcode year)cluster(lid)
		est store s2b
		label var zld `""Democracy" "age      ""'
		label var zl1polar "Polarization"
		label var zl1supdem `""Support for" "democracy""'
		label var zl1v2xps_party `""Party " "system" "instit. ""'
		label var zwealth `""GDP" "pc  ""'
		label var zl1v2elpubfin `""{bf:Publicly}   " "{bf:financed}  " "{bf:campaigns}""'
  		coefplot  (s2a, msymbol(O))(s2b, msymbol(T)), drop(_cons)  grid(glcolor(gs15))xline(0,lpattern(dash))  ///
			xtitle(Coefficient estimates) order(zl1v2elpubfin)level(90) ///
			title("Selection into personalist party rule",  ///
			size(medium)height(6)) xsize(2) ysize(3) mlabel format(%9.2g)  ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.65)xlab(-.15(.05).1) ///
			legend(lab(2 "Within, N=556") lab(4 "Within, N=358")pos(6)ring(1)col(2))
		gr export "$dir\golden\Ch2-selection.pdf",as(pdf)replace 
			
			
			* support for democracy trends *
			krls persparty ivdem ld l1supdem l4supdem 
			krls persparty ivdem ld devsupdem l4supdem 
			reg persparty ivdem ld devsupdem l4supdem,cluster(lid)
			reghdfe persparty ivdem ld l1supdem,a(cowcode)cluster(lid)
				

	erase h1.gph
	erase h2.gph
	erase h3.gph
	erase h4.gph
	************** THE END ******************

	log close
	
 
	
